package com.example.alibabavodossservice.vod;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * desc: VOD 常量信息
 *
 * @author xugenyin
 * desc:初始化bean后获取配置文件中的值交给静态变量
 */
@Component
public class InitVODConstants implements InitializingBean {
    @Value("${AliYun.keyId}")
    private String keyId;
    @Value("${AliYun.keySecret}")
    private String keySecret;
    /**
     * 访问密钥ID
     */
    public static String ACCESS_KEY_ID;
    /**
     * 访问密钥
     */
    public static String ACCESS_KEY_SECRET;
    /**
     * 点播地域
     */
    private static final String REGION_ID = "cn-shanghai";
    /**
     * 取值范围为100 KB~5 GB。默认值为100 KB
     */
    public static final int SLICE_SIZE = 1024 * 1024 * 1024 * 4;
    /**
     * OOS自主解析 上传文件存储空间
     */
    public static String BUCKET_NAME;
    /**
     * OOS自主解析 上传文件存储位置
     */
    public static String OBJECT_NAME;
    /**
     * STS临时访问凭证
     */
    private static String STS;
    /**
     * 端点(地域)
     */
    private static String ENDPOINT;
    /**
     * 阿里云视频点播服务客户端
     */
    public static DefaultAcsClient VOD_CLIENT = initVodClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET);


    /**
     * 初始化OSS客户端
     *
     * @param uploadAuth    上传凭证
     * @param uploadAddress 上传地址
     * @return OSSClient OSSClient
     */
    public static OSSClient initOssClient(JSONObject uploadAuth, JSONObject uploadAddress) {
        extractUploadAuth(uploadAuth, uploadAddress);
        //默认凭据提供程序
        DefaultCredentialProvider defaultCredentialProvider = new DefaultCredentialProvider(ACCESS_KEY_ID, ACCESS_KEY_SECRET, STS);
        return new OSSClient(ENDPOINT, defaultCredentialProvider, null);
    }

    /**
     * 使用AccessKey初始化vod客户端【长期、静态的访问授权】
     *
     * @param accessKeyId     accessKeyId
     * @param accessKeySecret accessKeySecret
     * @return DefaultAcsClient DefaultAcsClient
     */
    private static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) {
        return new DefaultAcsClient(DefaultProfile.getProfile(REGION_ID, accessKeyId, accessKeySecret));
    }

    /**
     * 使用STS初始化vod客户端【临时、动态的访问授权】
     * <p>阿里云STS（Security Token Service）是阿里云提供的一种临时访问权限管理服务。RAM提供RAM用户和RAM角色两种身份</p>
     * <p>其中，RAM角色不具备永久身份凭证，而只能通过STS获取可以自定义时效和访问权限的临时身份凭证，即安全令牌（STS Token）</p>
     *
     * @param accessKeyId     accessKeyId
     * @param accessKeySecret accessKeySecret
     * @param securityToken   STS-TOKEN
     * @return DefaultAcsClient DefaultAcsClient
     */
    private static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret, String securityToken) {
        return new DefaultAcsClient(DefaultProfile.getProfile(
                REGION_ID,
                System.getenv(accessKeyId),
                System.getenv(accessKeySecret),
                System.getenv(securityToken)));
    }

    /**
     * 解析JsonObject获取STS上传凭证
     *
     * @param uploadAuth    上传凭证
     * @param uploadAddress 上传地址
     */
    public static void extractUploadAuth(JSONObject uploadAuth, JSONObject uploadAddress) {
        ACCESS_KEY_ID = uploadAuth.getString("AccessKeyId");
        ACCESS_KEY_SECRET = uploadAuth.getString("AccessKeySecret");
        STS = uploadAuth.getString("SecurityToken");
        ENDPOINT = uploadAddress.getString("Endpoint");
    }

    /**
     * 解析JsonObject获取桶空间与位置
     *
     * @param uploadAddress 上传地址JSON
     */
    public static void extractUploadAddress(JSONObject uploadAddress) {
        BUCKET_NAME = uploadAddress.getString("Bucket");
        OBJECT_NAME = uploadAddress.getString("FileName");
    }

    @Override
    public void afterPropertiesSet() {
        ACCESS_KEY_ID = keyId;
        ACCESS_KEY_SECRET = keySecret;
        VOD_CLIENT = initVodClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET);

    }


}
